{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:13.373908Z",
     "start_time": "2024-11-28T11:50:13.370412Z"
    }
   },
   "source": [
    "import gc    # 垃圾回收\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# 导入分析库\n",
    "# 数据拆分\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 同分布数据拆分\n",
    "from sklearn.model_selection import StratifiedGroupKFold\n",
    "import lightgbm as lgb\n",
    "import xgboost as xgb"
   ],
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.180139Z",
     "start_time": "2024-11-28T11:50:13.400769Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "# 加载数据\n",
    "# 用户行为日志\n",
    "user_log = pd.read_csv('user_log_format1.csv', dtype = {'time_stamp':'str'})\n",
    "# 用户画像\n",
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "# 训练数据和测试数据\n",
    "train_data = pd.read_csv('train_format1.csv')\n",
    "test_data = pd.read_csv('test_format1.csv')"
   ],
   "id": "fb47359f0610aec4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 4.56 s\n",
      "Wall time: 22.8 s\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.188069Z",
     "start_time": "2024-11-28T11:50:36.181115Z"
    }
   },
   "cell_type": "code",
   "source": "test_data",
   "id": "e1c60888afef8bb0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id  merchant_id  prob\n",
       "0        163968         4605   NaN\n",
       "1        360576         1581   NaN\n",
       "2         98688         1964   NaN\n",
       "3         98688         3645   NaN\n",
       "4        295296         3361   NaN\n",
       "...         ...          ...   ...\n",
       "261472   228479         3111   NaN\n",
       "261473    97919         2341   NaN\n",
       "261474    97919         3971   NaN\n",
       "261475    32639         3536   NaN\n",
       "261476    32639         3319   NaN\n",
       "\n",
       "[261477 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>prob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98688</td>\n",
       "      <td>1964</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98688</td>\n",
       "      <td>3645</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>295296</td>\n",
       "      <td>3361</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>261472</th>\n",
       "      <td>228479</td>\n",
       "      <td>3111</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>261473</th>\n",
       "      <td>97919</td>\n",
       "      <td>2341</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>261474</th>\n",
       "      <td>97919</td>\n",
       "      <td>3971</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>261475</th>\n",
       "      <td>32639</td>\n",
       "      <td>3536</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>261476</th>\n",
       "      <td>32639</td>\n",
       "      <td>3319</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>261477 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.191793Z",
     "start_time": "2024-11-28T11:50:36.188069Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('---data shape---')     \n",
    "for data in [user_log, user_info, train_data, test_data]:\n",
    "    print(data.shape)"
   ],
   "id": "b6edb458a0cd8888",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---data shape---\n",
      "(54925330, 7)\n",
      "(424170, 3)\n",
      "(260864, 3)\n",
      "(261477, 3)\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.208750Z",
     "start_time": "2024-11-28T11:50:36.192770Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('---data info ---')\n",
    "for data in [user_log, user_info, train_data, test_data]:\n",
    "    print(data.info())"
   ],
   "id": "8d5d766a829263d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---data info ---\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   object \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(5), object(1)\n",
      "memory usage: 2.9+ GB\n",
      "None\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n",
      "None\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 260864 entries, 0 to 260863\n",
      "Data columns (total 3 columns):\n",
      " #   Column       Non-Null Count   Dtype\n",
      "---  ------       --------------   -----\n",
      " 0   user_id      260864 non-null  int64\n",
      " 1   merchant_id  260864 non-null  int64\n",
      " 2   label        260864 non-null  int64\n",
      "dtypes: int64(3)\n",
      "memory usage: 6.0 MB\n",
      "None\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 261477 entries, 0 to 261476\n",
      "Data columns (total 3 columns):\n",
      " #   Column       Non-Null Count   Dtype  \n",
      "---  ------       --------------   -----  \n",
      " 0   user_id      261477 non-null  int64  \n",
      " 1   merchant_id  261477 non-null  int64  \n",
      " 2   prob         0 non-null       float64\n",
      "dtypes: float64(1), int64(2)\n",
      "memory usage: 6.0 MB\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.215935Z",
     "start_time": "2024-11-28T11:50:36.209727Z"
    }
   },
   "cell_type": "code",
   "source": "display(user_info.head())",
   "id": "e46e54316a706c7a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  age_range  gender\n",
       "0   376517        6.0     1.0\n",
       "1   234512        5.0     0.0\n",
       "2   344532        5.0     0.0\n",
       "3   186135        5.0     0.0\n",
       "4    30230        5.0     0.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>376517</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>234512</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>344532</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>186135</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>30230</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.223662Z",
     "start_time": "2024-11-28T11:50:36.215935Z"
    }
   },
   "cell_type": "code",
   "source": "display(train_data.head(),test_data.head())",
   "id": "1a9f354dabe3b3dc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id  label\n",
       "0    34176         3906      0\n",
       "1    34176          121      0\n",
       "2    34176         4356      1\n",
       "3    34176         2217      0\n",
       "4   230784         4818      0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id  prob\n",
       "0   163968         4605   NaN\n",
       "1   360576         1581   NaN\n",
       "2    98688         1964   NaN\n",
       "3    98688         3645   NaN\n",
       "4   295296         3361   NaN"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>prob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98688</td>\n",
       "      <td>1964</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98688</td>\n",
       "      <td>3645</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>295296</td>\n",
       "      <td>3361</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.249977Z",
     "start_time": "2024-11-28T11:50:36.224640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_data['origin'] = 'train'\n",
    "test_data['origin'] = 'test'\n",
    "# 集成\n",
    "all_data = pd.concat([train_data, test_data], ignore_index=True, sort=False)\n",
    "# prob测试数据中特有的一列\n",
    "all_data.drop(['prob'], axis=1, inplace=True) # 删除概率这一列\n",
    "display(all_data.head(),all_data.shape)"
   ],
   "id": "2ccc3835da2ec1b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id  label origin\n",
       "0    34176         3906    0.0  train\n",
       "1    34176          121    0.0  train\n",
       "2    34176         4356    1.0  train\n",
       "3    34176         2217    0.0  train\n",
       "4   230784         4818    0.0  train"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(522341, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.338848Z",
     "start_time": "2024-11-28T11:50:36.249977Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 连接user_info表，通过user_id关联\n",
    "all_data = all_data.merge(user_info, on='user_id', how='left')\n",
    "display(all_data.shape,all_data.head())"
   ],
   "id": "9db8bacda77e916",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(522341, 6)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id  label origin  age_range  gender\n",
       "0    34176         3906    0.0  train        6.0     0.0\n",
       "1    34176          121    0.0  train        6.0     0.0\n",
       "2    34176         4356    1.0  train        6.0     0.0\n",
       "3    34176         2217    0.0  train        6.0     0.0\n",
       "4   230784         4818    0.0  train        0.0     0.0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.342857Z",
     "start_time": "2024-11-28T11:50:36.339846Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用 merchant_id（原列名seller_id）\n",
    "user_log.rename(columns={'seller_id':'merchant_id'}, inplace=True)"
   ],
   "id": "47eed0cb5688b4d9",
   "outputs": [],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.430115Z",
     "start_time": "2024-11-28T11:50:36.344815Z"
    }
   },
   "cell_type": "code",
   "source": [
    "del train_data,test_data,user_info\n",
    "gc.collect()"
   ],
   "id": "51e0bbfe70952f4d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "974"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.437675Z",
     "start_time": "2024-11-28T11:50:36.431091Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "display(user_log.info())"
   ],
   "id": "5460a1f729fe0395",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   merchant_id  int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   object \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(5), object(1)\n",
      "memory usage: 2.9+ GB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "None"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 0 ns\n",
      "Wall time: 3.89 ms\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:36.446170Z",
     "start_time": "2024-11-28T11:50:36.438653Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "display(user_log.head())"
   ],
   "id": "1eeb228f4d9864a2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  item_id  cat_id  merchant_id  brand_id time_stamp  action_type\n",
       "0   328862   323294     833         2882    2661.0       0829            0\n",
       "1   328862   844400    1271         2882    2661.0       0829            0\n",
       "2   328862   575153    1271         2882    2661.0       0829            0\n",
       "3   328862   996875    1271         2882    2661.0       0829            0\n",
       "4   328862  1086186    1271         1253    1049.0       0829            0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>328862</td>\n",
       "      <td>323294</td>\n",
       "      <td>833</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>0829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>328862</td>\n",
       "      <td>844400</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>0829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>328862</td>\n",
       "      <td>575153</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>0829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>328862</td>\n",
       "      <td>996875</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>0829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>328862</td>\n",
       "      <td>1086186</td>\n",
       "      <td>1271</td>\n",
       "      <td>1253</td>\n",
       "      <td>1049.0</td>\n",
       "      <td>0829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 0 ns\n",
      "Wall time: 3.9 ms\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:41.291671Z",
     "start_time": "2024-11-28T11:50:36.447169Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "# 用户行为数据类型转换\n",
    "user_log['user_id'] = user_log['user_id'].astype('int32')\n",
    "user_log['merchant_id'] = user_log['merchant_id'].astype('int32')\n",
    "user_log['item_id'] = user_log['item_id'].astype('int32')\n",
    "user_log['cat_id'] = user_log['cat_id'].astype('int32')\n",
    "user_log['brand_id'].fillna(0, inplace=True)\n",
    "user_log['brand_id'] = user_log['brand_id'].astype('int32')\n",
    "user_log['time_stamp'] = pd.to_datetime(user_log['time_stamp'], format='%H%M')\n",
    "user_log['action_type'] = user_log['action_type'].astype('int32')\n",
    "display(user_log.info(),user_log.head())"
   ],
   "id": "d1fcffc532d684",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype         \n",
      "---  ------       -----         \n",
      " 0   user_id      int32         \n",
      " 1   item_id      int32         \n",
      " 2   cat_id       int32         \n",
      " 3   merchant_id  int32         \n",
      " 4   brand_id     int32         \n",
      " 5   time_stamp   datetime64[ns]\n",
      " 6   action_type  int32         \n",
      "dtypes: datetime64[ns](1), int32(6)\n",
      "memory usage: 1.6 GB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "None"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "   user_id  item_id  cat_id  merchant_id  brand_id          time_stamp  \\\n",
       "0   328862   323294     833         2882      2661 1900-01-01 08:29:00   \n",
       "1   328862   844400    1271         2882      2661 1900-01-01 08:29:00   \n",
       "2   328862   575153    1271         2882      2661 1900-01-01 08:29:00   \n",
       "3   328862   996875    1271         2882      2661 1900-01-01 08:29:00   \n",
       "4   328862  1086186    1271         1253      1049 1900-01-01 08:29:00   \n",
       "\n",
       "   action_type  \n",
       "0            0  \n",
       "1            0  \n",
       "2            0  \n",
       "3            0  \n",
       "4            0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>328862</td>\n",
       "      <td>323294</td>\n",
       "      <td>833</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661</td>\n",
       "      <td>1900-01-01 08:29:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>328862</td>\n",
       "      <td>844400</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661</td>\n",
       "      <td>1900-01-01 08:29:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>328862</td>\n",
       "      <td>575153</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661</td>\n",
       "      <td>1900-01-01 08:29:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>328862</td>\n",
       "      <td>996875</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661</td>\n",
       "      <td>1900-01-01 08:29:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>328862</td>\n",
       "      <td>1086186</td>\n",
       "      <td>1271</td>\n",
       "      <td>1253</td>\n",
       "      <td>1049</td>\n",
       "      <td>1900-01-01 08:29:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 547 ms\n",
      "Wall time: 4.84 s\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:41.318860Z",
     "start_time": "2024-11-28T11:50:41.292648Z"
    }
   },
   "cell_type": "code",
   "source": "display(all_data.isnull().sum())",
   "id": "2ad44ab263ee483b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id             0\n",
       "merchant_id         0\n",
       "label          261477\n",
       "origin              0\n",
       "age_range        2578\n",
       "gender           7545\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:41.341388Z",
     "start_time": "2024-11-28T11:50:41.318860Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 缺失值填充\n",
    "all_data['age_range'].fillna(0, inplace=True)\n",
    "all_data['gender'].fillna(2, inplace=True)\n",
    "all_data.isnull().sum()"
   ],
   "id": "8a47f5223d147a0a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id             0\n",
       "merchant_id         0\n",
       "label          261477\n",
       "origin              0\n",
       "age_range           0\n",
       "gender              0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:41.363901Z",
     "start_time": "2024-11-28T11:50:41.342372Z"
    }
   },
   "cell_type": "code",
   "source": "all_data.info()",
   "id": "2362df5d94a38095",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 522341 entries, 0 to 522340\n",
      "Data columns (total 6 columns):\n",
      " #   Column       Non-Null Count   Dtype  \n",
      "---  ------       --------------   -----  \n",
      " 0   user_id      522341 non-null  int64  \n",
      " 1   merchant_id  522341 non-null  int64  \n",
      " 2   label        260864 non-null  float64\n",
      " 3   origin       522341 non-null  object \n",
      " 4   age_range    522341 non-null  float64\n",
      " 5   gender       522341 non-null  float64\n",
      "dtypes: float64(3), int64(2), object(1)\n",
      "memory usage: 23.9+ MB\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:50:41.553767Z",
     "start_time": "2024-11-28T11:50:41.364890Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_data['age_range'] = all_data['age_range'].astype('int8')\n",
    "all_data['gender'] = all_data['gender'].astype('int8')\n",
    "all_data['label'] = all_data['label'].astype('str')\n",
    "all_data['user_id'] = all_data['user_id'].astype('int32')\n",
    "all_data['merchant_id'] = all_data['merchant_id'].astype('int32')\n",
    "all_data.info()"
   ],
   "id": "e9019badc97c944d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 522341 entries, 0 to 522340\n",
      "Data columns (total 6 columns):\n",
      " #   Column       Non-Null Count   Dtype \n",
      "---  ------       --------------   ----- \n",
      " 0   user_id      522341 non-null  int32 \n",
      " 1   merchant_id  522341 non-null  int32 \n",
      " 2   label        522341 non-null  object\n",
      " 3   origin       522341 non-null  object\n",
      " 4   age_range    522341 non-null  int8  \n",
      " 5   gender       522341 non-null  int8  \n",
      "dtypes: int32(2), int8(2), object(2)\n",
      "memory usage: 13.0+ MB\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:51:00.916619Z",
     "start_time": "2024-11-28T11:50:41.554750Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "##### 特征处理\n",
    "##### User特征处理\n",
    "groups = user_log.groupby(['user_id'])\n",
    " \n",
    "# 用户交互行为数量 u1\n",
    "temp = groups.size().reset_index().rename(columns={0:'u1'})\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    "# 细分\n",
    "# 使用 agg 基于列的聚合操作，统计唯一值个数 item_id, cat_id, merchant_id, brand_id\n",
    "# 用户，交互行为：点了多少商品呢？\n",
    "temp = groups['item_id'].agg([('u2', 'nunique')]).reset_index()\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    "# 用户，交互行为，具体统计：类目多少\n",
    "temp = groups['cat_id'].agg([('u3', 'nunique')]).reset_index()\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    "temp = groups['merchant_id'].agg([('u4', 'nunique')]).reset_index()\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    "temp = groups['brand_id'].agg([('u5', 'nunique')]).reset_index()\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    " \n",
    "# 购物时间间隔特征 u6 按照小时\n",
    "temp = groups['time_stamp'].agg([('F_time', 'min'), ('B_time', 'max')]).reset_index()\n",
    "temp['u6'] = (temp['B_time'] - temp['F_time']).dt.seconds/3600\n",
    "all_data = all_data.merge(temp[['user_id', 'u6']], on='user_id', how='left')\n",
    " \n",
    " \n",
    "# 统计操作类型为0，1，2，3的个数\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(\n",
    "    columns={0:'u7', 1:'u8', 2:'u9', 3:'u10'})\n",
    "all_data = all_data.merge(temp, on='user_id', how='left')\n",
    " \n",
    "del temp,groups\n",
    "gc.collect()"
   ],
   "id": "a489560c7af5fcf5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 3.44 s\n",
      "Wall time: 19.4 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:51:00.924619Z",
     "start_time": "2024-11-28T11:51:00.917597Z"
    }
   },
   "cell_type": "code",
   "source": "all_data.head()",
   "id": "eee9b7e7d3f014a5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id label origin  age_range  gender   u1   u2  u3   u4  \\\n",
       "0    34176         3906   0.0  train          6       0  451  256  45  109   \n",
       "1    34176          121   0.0  train          6       0  451  256  45  109   \n",
       "2    34176         4356   1.0  train          6       0  451  256  45  109   \n",
       "3    34176         2217   0.0  train          6       0  451  256  45  109   \n",
       "4   230784         4818   0.0  train          0       0   54   31  17   20   \n",
       "\n",
       "    u5        u6     u7  u8    u9  u10  \n",
       "0  108  5.833333  410.0 NaN  34.0  7.0  \n",
       "1  108  5.833333  410.0 NaN  34.0  7.0  \n",
       "2  108  5.833333  410.0 NaN  34.0  7.0  \n",
       "3  108  5.833333  410.0 NaN  34.0  7.0  \n",
       "4   19  5.166667   47.0 NaN   7.0  NaN  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>u5</th>\n",
       "      <th>u6</th>\n",
       "      <th>u7</th>\n",
       "      <th>u8</th>\n",
       "      <th>u9</th>\n",
       "      <th>u10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>410.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>410.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>410.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>410.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>34.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>19</td>\n",
       "      <td>5.166667</td>\n",
       "      <td>47.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:52:46.082886Z",
     "start_time": "2024-11-28T11:52:37.289955Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "##### 商家特征处理\n",
    "groups = user_log.groupby(['merchant_id'])\n",
    " \n",
    "# 商家被交互行为数量 m1\n",
    "temp = groups.size().reset_index().rename(columns={0:'m1'})\n",
    "all_data = all_data.merge(temp, on='merchant_id', how='left')\n",
    " \n",
    "# 统计商家被交互的 user_id, item_id, cat_id, brand_id 唯一值\n",
    "temp = groups[['user_id', 'item_id', 'cat_id', 'brand_id']].nunique().reset_index().rename(\n",
    "    columns={\n",
    "    'user_id':'m2',\n",
    "    'item_id':'m3', \n",
    "    'cat_id':'m4', \n",
    "    'brand_id':'m5'})\n",
    "all_data = all_data.merge(temp, on='merchant_id', how='left')\n",
    " \n",
    "# 统计商家被交互的 action_type 唯一值\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(  \n",
    "    columns={0:'m6', 1:'m7', 2:'m8', 3:'m9'})\n",
    "all_data = all_data.merge(temp, on='merchant_id', how='left')\n",
    " \n",
    "del temp\n",
    "gc.collect()"
   ],
   "id": "82be614fe18d35ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 3.06 s\n",
      "Wall time: 8.79 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:52:57.939286Z",
     "start_time": "2024-11-28T11:52:57.924634Z"
    }
   },
   "cell_type": "code",
   "source": "display(all_data.tail())",
   "id": "c0cd83b224008d60",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id  merchant_id label origin  age_range  gender    u1    u2  u3  \\\n",
       "522336   228479         3111   nan   test          6       0  2004  1173  71   \n",
       "522337    97919         2341   nan   test          8       1    55    29  14   \n",
       "522338    97919         3971   nan   test          8       1    55    29  14   \n",
       "522339    32639         3536   nan   test          0       0    72    46  24   \n",
       "522340    32639         3319   nan   test          0       0    72    46  24   \n",
       "\n",
       "         u4  ...   m1_x   m1_y    m2   m3   m4  m5       m6    m7      m8  \\\n",
       "522336  278  ...  10105  10105  4154  542   50  18   8997.0   9.0   687.0   \n",
       "522337   17  ...   5543   5543  1592  352   93  19   4548.0   6.0   815.0   \n",
       "522338   17  ...  28892  28892  7587  272    7   2  24602.0  94.0  2608.0   \n",
       "522339   33  ...  14027  14027  4956  322   19   3  12807.0  29.0   793.0   \n",
       "522340   33  ...  25959  25959  7927  952  175  85  21737.0  34.0  2700.0   \n",
       "\n",
       "            m9  \n",
       "522336   412.0  \n",
       "522337   174.0  \n",
       "522338  1588.0  \n",
       "522339   398.0  \n",
       "522340  1488.0  \n",
       "\n",
       "[5 rows x 26 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>...</th>\n",
       "      <th>m1_x</th>\n",
       "      <th>m1_y</th>\n",
       "      <th>m2</th>\n",
       "      <th>m3</th>\n",
       "      <th>m4</th>\n",
       "      <th>m5</th>\n",
       "      <th>m6</th>\n",
       "      <th>m7</th>\n",
       "      <th>m8</th>\n",
       "      <th>m9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>522336</th>\n",
       "      <td>228479</td>\n",
       "      <td>3111</td>\n",
       "      <td>nan</td>\n",
       "      <td>test</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2004</td>\n",
       "      <td>1173</td>\n",
       "      <td>71</td>\n",
       "      <td>278</td>\n",
       "      <td>...</td>\n",
       "      <td>10105</td>\n",
       "      <td>10105</td>\n",
       "      <td>4154</td>\n",
       "      <td>542</td>\n",
       "      <td>50</td>\n",
       "      <td>18</td>\n",
       "      <td>8997.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>687.0</td>\n",
       "      <td>412.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522337</th>\n",
       "      <td>97919</td>\n",
       "      <td>2341</td>\n",
       "      <td>nan</td>\n",
       "      <td>test</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>55</td>\n",
       "      <td>29</td>\n",
       "      <td>14</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>5543</td>\n",
       "      <td>5543</td>\n",
       "      <td>1592</td>\n",
       "      <td>352</td>\n",
       "      <td>93</td>\n",
       "      <td>19</td>\n",
       "      <td>4548.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>815.0</td>\n",
       "      <td>174.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522338</th>\n",
       "      <td>97919</td>\n",
       "      <td>3971</td>\n",
       "      <td>nan</td>\n",
       "      <td>test</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>55</td>\n",
       "      <td>29</td>\n",
       "      <td>14</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>28892</td>\n",
       "      <td>28892</td>\n",
       "      <td>7587</td>\n",
       "      <td>272</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>24602.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>2608.0</td>\n",
       "      <td>1588.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522339</th>\n",
       "      <td>32639</td>\n",
       "      <td>3536</td>\n",
       "      <td>nan</td>\n",
       "      <td>test</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>46</td>\n",
       "      <td>24</td>\n",
       "      <td>33</td>\n",
       "      <td>...</td>\n",
       "      <td>14027</td>\n",
       "      <td>14027</td>\n",
       "      <td>4956</td>\n",
       "      <td>322</td>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>12807.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>793.0</td>\n",
       "      <td>398.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>522340</th>\n",
       "      <td>32639</td>\n",
       "      <td>3319</td>\n",
       "      <td>nan</td>\n",
       "      <td>test</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>46</td>\n",
       "      <td>24</td>\n",
       "      <td>33</td>\n",
       "      <td>...</td>\n",
       "      <td>25959</td>\n",
       "      <td>25959</td>\n",
       "      <td>7927</td>\n",
       "      <td>952</td>\n",
       "      <td>175</td>\n",
       "      <td>85</td>\n",
       "      <td>21737.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>2700.0</td>\n",
       "      <td>1488.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:08.069857Z",
     "start_time": "2024-11-28T11:56:06.030617Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "##### 用户+商户特征\n",
    "groups = user_log.groupby(['user_id', 'merchant_id'])\n",
    " \n",
    "# 用户在不同商家交互统计\n",
    "temp = groups.size().reset_index().rename(columns={0:'um1'})\n",
    "all_data = all_data.merge(temp, on=['user_id', 'merchant_id'], how='left')\n",
    " \n",
    "# 统计用户在不同商家交互的 item_id, cat_id, brand_id 唯一值\n",
    "temp = groups[['item_id', 'cat_id', 'brand_id']].nunique().reset_index().rename(\n",
    "    columns={\n",
    "    'item_id':'um2',\n",
    "    'cat_id':'um3',\n",
    "    'brand_id':'um4'})\n",
    "all_data = all_data.merge(temp, on=['user_id', 'merchant_id'], how='left')\n",
    " \n",
    "# 统计用户在不同商家交互的 action_type 唯一值\n",
    "temp = groups['action_type'].value_counts().unstack().reset_index().rename(\n",
    "    columns={\n",
    "    0:'um5',\n",
    "    1:'um6',\n",
    "    2:'um7',\n",
    "    3:'um8'})\n",
    "all_data = all_data.merge(temp, on=['user_id', 'merchant_id'], how='left')\n",
    " \n",
    "# 统计用户在不同商家购物时间间隔特征 um9 按照小时\n",
    "temp = groups['time_stamp'].agg([('F_time', 'min'), ('B_time', 'max')]).reset_index()\n",
    "temp['um9'] = (temp['B_time'] - temp['F_time']).dt.seconds/3600\n",
    "all_data = all_data.merge(temp[['user_id','merchant_id','um9']], on=['user_id', 'merchant_id'], how='left')\n",
    " \n",
    "del temp,groups\n",
    "gc.collect()"
   ],
   "id": "f43e5f247b7e2820",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 26.3 s\n",
      "Wall time: 1min 2s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:13.265697Z",
     "start_time": "2024-11-28T11:57:13.256419Z"
    }
   },
   "cell_type": "code",
   "source": "display(all_data.head())",
   "id": "971173d0363ed737",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id label origin  age_range  gender   u1   u2  u3   u4  \\\n",
       "0    34176         3906   0.0  train          6       0  451  256  45  109   \n",
       "1    34176          121   0.0  train          6       0  451  256  45  109   \n",
       "2    34176         4356   1.0  train          6       0  451  256  45  109   \n",
       "3    34176         2217   0.0  train          6       0  451  256  45  109   \n",
       "4   230784         4818   0.0  train          0       0   54   31  17   20   \n",
       "\n",
       "   ...  um1_x  um1_y  um2  um3  um4   um5  um6  um7  um8       um9  \n",
       "0  ...     39     39   20    6    1  36.0  NaN  1.0  2.0  0.850000  \n",
       "1  ...     14     14    1    1    1  13.0  NaN  1.0  NaN  0.050000  \n",
       "2  ...     18     18    2    1    1  12.0  NaN  6.0  NaN  0.016667  \n",
       "3  ...      2      2    1    1    1   1.0  NaN  1.0  NaN  0.000000  \n",
       "4  ...      8      8    1    1    1   7.0  NaN  1.0  NaN  0.050000  \n",
       "\n",
       "[5 rows x 36 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>...</th>\n",
       "      <th>um1_x</th>\n",
       "      <th>um1_y</th>\n",
       "      <th>um2</th>\n",
       "      <th>um3</th>\n",
       "      <th>um4</th>\n",
       "      <th>um5</th>\n",
       "      <th>um6</th>\n",
       "      <th>um7</th>\n",
       "      <th>um8</th>\n",
       "      <th>um9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>39</td>\n",
       "      <td>39</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>36.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.850000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>14</td>\n",
       "      <td>14</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>13.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.050000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.016667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>...</td>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.050000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:15.144130Z",
     "start_time": "2024-11-28T11:57:15.126035Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_data['r1'] = all_data['u9']/all_data['u7']    # 用户购买点击比\n",
    "all_data['r2'] = all_data['m8']/all_data['m6']    # 商家购买点击比\n",
    "all_data['r3'] = all_data['um7']/all_data['um5']  # 不同用户不同商家购买点击比\n",
    "display(all_data.head())"
   ],
   "id": "1dd84003bf47a41f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id label origin  age_range  gender   u1   u2  u3   u4  \\\n",
       "0    34176         3906   0.0  train          6       0  451  256  45  109   \n",
       "1    34176          121   0.0  train          6       0  451  256  45  109   \n",
       "2    34176         4356   1.0  train          6       0  451  256  45  109   \n",
       "3    34176         2217   0.0  train          6       0  451  256  45  109   \n",
       "4   230784         4818   0.0  train          0       0   54   31  17   20   \n",
       "\n",
       "   ...  um3  um4   um5  um6  um7  um8       um9        r1        r2        r3  \n",
       "0  ...    6    1  36.0  NaN  1.0  2.0  0.850000  0.082927  0.027572  0.027778  \n",
       "1  ...    1    1  13.0  NaN  1.0  NaN  0.050000  0.082927  0.066145  0.076923  \n",
       "2  ...    1    1  12.0  NaN  6.0  NaN  0.016667  0.082927  0.158024  0.500000  \n",
       "3  ...    1    1   1.0  NaN  1.0  NaN  0.000000  0.082927  0.071243  1.000000  \n",
       "4  ...    1    1   7.0  NaN  1.0  NaN  0.050000  0.148936  0.063164  0.142857  \n",
       "\n",
       "[5 rows x 39 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>...</th>\n",
       "      <th>um3</th>\n",
       "      <th>um4</th>\n",
       "      <th>um5</th>\n",
       "      <th>um6</th>\n",
       "      <th>um7</th>\n",
       "      <th>um8</th>\n",
       "      <th>um9</th>\n",
       "      <th>r1</th>\n",
       "      <th>r2</th>\n",
       "      <th>r3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>36.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.850000</td>\n",
       "      <td>0.082927</td>\n",
       "      <td>0.027572</td>\n",
       "      <td>0.027778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>13.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.082927</td>\n",
       "      <td>0.066145</td>\n",
       "      <td>0.076923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.016667</td>\n",
       "      <td>0.082927</td>\n",
       "      <td>0.158024</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.082927</td>\n",
       "      <td>0.071243</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.050000</td>\n",
       "      <td>0.148936</td>\n",
       "      <td>0.063164</td>\n",
       "      <td>0.142857</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:17.183003Z",
     "start_time": "2024-11-28T11:57:17.137360Z"
    }
   },
   "cell_type": "code",
   "source": "display(all_data.isnull().sum())",
   "id": "d4154a772241ae56",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id             0\n",
       "merchant_id         0\n",
       "label               0\n",
       "origin              0\n",
       "age_range           0\n",
       "gender              0\n",
       "u1                  0\n",
       "u2                  0\n",
       "u3                  0\n",
       "u4                  0\n",
       "u5                  0\n",
       "u6                  0\n",
       "u7                360\n",
       "u8             484162\n",
       "u9                  0\n",
       "u10            227482\n",
       "m1_x                0\n",
       "m1_y                0\n",
       "m2                  0\n",
       "m3                  0\n",
       "m4                  0\n",
       "m5                  0\n",
       "m6                  0\n",
       "m7               4052\n",
       "m8                  0\n",
       "m9                  0\n",
       "um1_x               0\n",
       "um1_y               0\n",
       "um2                 0\n",
       "um3                 0\n",
       "um4                 0\n",
       "um5             59408\n",
       "um6            512947\n",
       "um7                 0\n",
       "um8            425790\n",
       "um9                 0\n",
       "r1                360\n",
       "r2                  0\n",
       "r3              59408\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:19.195483Z",
     "start_time": "2024-11-28T11:57:19.063548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_data.fillna(0, inplace=True)\n",
    "all_data.isnull().sum()"
   ],
   "id": "5d4f1cd1de41c346",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id        0\n",
       "merchant_id    0\n",
       "label          0\n",
       "origin         0\n",
       "age_range      0\n",
       "gender         0\n",
       "u1             0\n",
       "u2             0\n",
       "u3             0\n",
       "u4             0\n",
       "u5             0\n",
       "u6             0\n",
       "u7             0\n",
       "u8             0\n",
       "u9             0\n",
       "u10            0\n",
       "m1_x           0\n",
       "m1_y           0\n",
       "m2             0\n",
       "m3             0\n",
       "m4             0\n",
       "m5             0\n",
       "m6             0\n",
       "m7             0\n",
       "m8             0\n",
       "m9             0\n",
       "um1_x          0\n",
       "um1_y          0\n",
       "um2            0\n",
       "um3            0\n",
       "um4            0\n",
       "um5            0\n",
       "um6            0\n",
       "um7            0\n",
       "um8            0\n",
       "um9            0\n",
       "r1             0\n",
       "r2             0\n",
       "r3             0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:21.180584Z",
     "start_time": "2024-11-28T11:57:21.176411Z"
    }
   },
   "cell_type": "code",
   "source": "all_data['age_range']",
   "id": "700a389cc199c646",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         6\n",
       "1         6\n",
       "2         6\n",
       "3         6\n",
       "4         0\n",
       "         ..\n",
       "522336    6\n",
       "522337    8\n",
       "522338    8\n",
       "522339    0\n",
       "522340    0\n",
       "Name: age_range, Length: 522341, dtype: int8"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:41.683690Z",
     "start_time": "2024-11-28T11:57:41.604066Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "# 修改age_range字段名称为 age_0, age_1, age_2... age_8\n",
    "# 独立编码\n",
    "temp = pd.get_dummies(all_data['age_range'], prefix='age')\n",
    "display(temp.head(10))\n",
    "all_data = pd.concat([all_data, temp], axis=1)"
   ],
   "id": "fcc809fc3d005107",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   age_0  age_1  age_2  age_3  age_4  age_5  age_6  age_7  age_8\n",
       "0  False  False  False  False  False  False   True  False  False\n",
       "1  False  False  False  False  False  False   True  False  False\n",
       "2  False  False  False  False  False  False   True  False  False\n",
       "3  False  False  False  False  False  False   True  False  False\n",
       "4   True  False  False  False  False  False  False  False  False\n",
       "5  False  False  False  False   True  False  False  False  False\n",
       "6  False  False  False  False  False   True  False  False  False\n",
       "7  False  False  False  False  False   True  False  False  False\n",
       "8  False  False  False  False  False   True  False  False  False\n",
       "9  False  False  False  False   True  False  False  False  False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age_0</th>\n",
       "      <th>age_1</th>\n",
       "      <th>age_2</th>\n",
       "      <th>age_3</th>\n",
       "      <th>age_4</th>\n",
       "      <th>age_5</th>\n",
       "      <th>age_6</th>\n",
       "      <th>age_7</th>\n",
       "      <th>age_8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 62.5 ms\n",
      "Wall time: 76.8 ms\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:51.393901Z",
     "start_time": "2024-11-28T11:57:46.863460Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 性别转换\n",
    "temp = pd.get_dummies(all_data['gender'], prefix='g')\n",
    "all_data = pd.concat([all_data, temp], axis=1) # 列进行合并\n",
    " \n",
    "# 删除原数据\n",
    "all_data.drop(['age_range', 'gender'], axis=1, inplace=True)\n",
    " \n",
    "del temp\n",
    "gc.collect()"
   ],
   "id": "bff163e1da63c98a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:57:56.739328Z",
     "start_time": "2024-11-28T11:57:56.730570Z"
    }
   },
   "cell_type": "code",
   "source": "all_data.head()",
   "id": "4d03f422b22ef3a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id label origin   u1   u2  u3   u4   u5        u6  ...  \\\n",
       "0    34176         3906   0.0  train  451  256  45  109  108  5.833333  ...   \n",
       "1    34176          121   0.0  train  451  256  45  109  108  5.833333  ...   \n",
       "2    34176         4356   1.0  train  451  256  45  109  108  5.833333  ...   \n",
       "3    34176         2217   0.0  train  451  256  45  109  108  5.833333  ...   \n",
       "4   230784         4818   0.0  train   54   31  17   20   19  5.166667  ...   \n",
       "\n",
       "   age_2  age_3  age_4  age_5  age_6  age_7  age_8   g_0    g_1    g_2  \n",
       "0  False  False  False  False   True  False  False  True  False  False  \n",
       "1  False  False  False  False   True  False  False  True  False  False  \n",
       "2  False  False  False  False   True  False  False  True  False  False  \n",
       "3  False  False  False  False   True  False  False  True  False  False  \n",
       "4  False  False  False  False  False  False  False  True  False  False  \n",
       "\n",
       "[5 rows x 49 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>origin</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>u5</th>\n",
       "      <th>u6</th>\n",
       "      <th>...</th>\n",
       "      <th>age_2</th>\n",
       "      <th>age_3</th>\n",
       "      <th>age_4</th>\n",
       "      <th>age_5</th>\n",
       "      <th>age_6</th>\n",
       "      <th>age_7</th>\n",
       "      <th>age_8</th>\n",
       "      <th>g_0</th>\n",
       "      <th>g_1</th>\n",
       "      <th>g_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>train</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>451</td>\n",
       "      <td>256</td>\n",
       "      <td>45</td>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>54</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>19</td>\n",
       "      <td>5.166667</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:58:29.240537Z",
     "start_time": "2024-11-28T11:58:23.735414Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "# train_data、test-data\n",
    "train_data = all_data[all_data['origin'] == 'train'].drop(['origin'], axis=1)\n",
    "test_data = all_data[all_data['origin'] == 'test'].drop(['label', 'origin'], axis=1)\n",
    " \n",
    "train_data.to_csv('train_data.csv')\n",
    "test_data.to_csv('test_data.csv')"
   ],
   "id": "eb98e6f0e584fcd7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 2.45 s\n",
      "Wall time: 5.5 s\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T11:58:43.170461Z",
     "start_time": "2024-11-28T11:58:43.088844Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 训练数据和目标值\n",
    "train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']\n",
    " \n",
    "# 数据拆分保留20%作为测试数据\n",
    "X_train, X_valid, y_train, y_valid = train_test_split(train_X, train_y, test_size=.2)"
   ],
   "id": "39778251f564cf4b",
   "outputs": [],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:05:27.568744Z",
     "start_time": "2024-11-28T12:05:27.564515Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import logging\n",
    "def lgb_train(X_train, y_train, X_valid, y_valid, verbose=True):\n",
    "    model_lgb = lgb.LGBMClassifier(\n",
    "        max_depth=10, # 8 # 树最大的深度\n",
    "        n_estimators=5000, # 集成算法，树数量\n",
    "        min_child_weight=100, \n",
    "        colsample_bytree=0.7, # 特征筛选\n",
    "        subsample=0.9,  # 样本采样比例\n",
    "        learning_rate=0.1) # 学习率\n",
    "    callbacks = [lgb.early_stopping(stopping_rounds=10)]\n",
    "    if verbose:\n",
    "        logging.basicConfig(level=logging.INFO)\n",
    "    else:\n",
    "        logging.basicConfig(level=logging.WARNING)\n",
    "    model_lgb.fit(\n",
    "        X_train, \n",
    "        y_train,\n",
    "        eval_metric='auc',\n",
    "        eval_set=[(X_train, y_train), (X_valid, y_valid)],\n",
    "        callbacks=callbacks) # 早停，等10轮决策，评价指标不在变化，停止\n",
    " \n",
    "    print(model_lgb.best_score_['valid_1']['auc'])\n",
    "    return model_lgb"
   ],
   "id": "42c49d3e0fa60b5",
   "outputs": [],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:02:19.429997Z",
     "start_time": "2024-11-28T12:02:19.398179Z"
    }
   },
   "cell_type": "code",
   "source": "X_train",
   "id": "cca5a49edab669c1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id  merchant_id   u1   u2  u3   u4   u5        u6     u7   u8  \\\n",
       "162196    57694          316   77   53  19   39   39  3.750000   70.0  0.0   \n",
       "59507      1839         2108  181  113  27   43   40  5.133333  175.0  0.0   \n",
       "194422   266429         4726  454  303  38  115  116  5.850000  433.0  1.0   \n",
       "211723   348911         1618  283  182  63  116  109  5.750000  263.0  0.0   \n",
       "155664   122443          416  137  109  31   36   36  5.900000   76.0  3.0   \n",
       "...         ...          ...  ...  ...  ..  ...  ...       ...    ...  ...   \n",
       "98809    252835         3723   17   10   6    5    5  5.083333   13.0  0.0   \n",
       "101888   186028         4044  305  176  50   81   82  5.816667  294.0  0.0   \n",
       "227266   377885         1501  362  235  64  120  119  5.966667  169.0  0.0   \n",
       "163230   194401          877   13    9   3    4    4  4.950000   12.0  0.0   \n",
       "240192   365891         1013   24   16   6   10   12  4.733333   22.0  0.0   \n",
       "\n",
       "        ...  age_2  age_3  age_4  age_5  age_6  age_7  age_8    g_0    g_1  \\\n",
       "162196  ...   True  False  False  False  False  False  False  False   True   \n",
       "59507   ...  False  False  False   True  False  False  False  False   True   \n",
       "194422  ...  False  False  False  False  False  False  False   True  False   \n",
       "211723  ...  False  False  False   True  False  False  False  False   True   \n",
       "155664  ...  False  False  False  False  False  False  False   True  False   \n",
       "...     ...    ...    ...    ...    ...    ...    ...    ...    ...    ...   \n",
       "98809   ...  False  False  False  False   True  False  False  False   True   \n",
       "101888  ...  False   True  False  False  False  False  False   True  False   \n",
       "227266  ...   True  False  False  False  False  False  False   True  False   \n",
       "163230  ...  False  False  False  False   True  False  False   True  False   \n",
       "240192  ...  False  False  False  False  False   True  False  False   True   \n",
       "\n",
       "          g_2  \n",
       "162196  False  \n",
       "59507   False  \n",
       "194422  False  \n",
       "211723  False  \n",
       "155664  False  \n",
       "...       ...  \n",
       "98809   False  \n",
       "101888  False  \n",
       "227266  False  \n",
       "163230  False  \n",
       "240192  False  \n",
       "\n",
       "[208691 rows x 47 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>u1</th>\n",
       "      <th>u2</th>\n",
       "      <th>u3</th>\n",
       "      <th>u4</th>\n",
       "      <th>u5</th>\n",
       "      <th>u6</th>\n",
       "      <th>u7</th>\n",
       "      <th>u8</th>\n",
       "      <th>...</th>\n",
       "      <th>age_2</th>\n",
       "      <th>age_3</th>\n",
       "      <th>age_4</th>\n",
       "      <th>age_5</th>\n",
       "      <th>age_6</th>\n",
       "      <th>age_7</th>\n",
       "      <th>age_8</th>\n",
       "      <th>g_0</th>\n",
       "      <th>g_1</th>\n",
       "      <th>g_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>162196</th>\n",
       "      <td>57694</td>\n",
       "      <td>316</td>\n",
       "      <td>77</td>\n",
       "      <td>53</td>\n",
       "      <td>19</td>\n",
       "      <td>39</td>\n",
       "      <td>39</td>\n",
       "      <td>3.750000</td>\n",
       "      <td>70.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59507</th>\n",
       "      <td>1839</td>\n",
       "      <td>2108</td>\n",
       "      <td>181</td>\n",
       "      <td>113</td>\n",
       "      <td>27</td>\n",
       "      <td>43</td>\n",
       "      <td>40</td>\n",
       "      <td>5.133333</td>\n",
       "      <td>175.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>194422</th>\n",
       "      <td>266429</td>\n",
       "      <td>4726</td>\n",
       "      <td>454</td>\n",
       "      <td>303</td>\n",
       "      <td>38</td>\n",
       "      <td>115</td>\n",
       "      <td>116</td>\n",
       "      <td>5.850000</td>\n",
       "      <td>433.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>211723</th>\n",
       "      <td>348911</td>\n",
       "      <td>1618</td>\n",
       "      <td>283</td>\n",
       "      <td>182</td>\n",
       "      <td>63</td>\n",
       "      <td>116</td>\n",
       "      <td>109</td>\n",
       "      <td>5.750000</td>\n",
       "      <td>263.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>155664</th>\n",
       "      <td>122443</td>\n",
       "      <td>416</td>\n",
       "      <td>137</td>\n",
       "      <td>109</td>\n",
       "      <td>31</td>\n",
       "      <td>36</td>\n",
       "      <td>36</td>\n",
       "      <td>5.900000</td>\n",
       "      <td>76.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98809</th>\n",
       "      <td>252835</td>\n",
       "      <td>3723</td>\n",
       "      <td>17</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5.083333</td>\n",
       "      <td>13.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101888</th>\n",
       "      <td>186028</td>\n",
       "      <td>4044</td>\n",
       "      <td>305</td>\n",
       "      <td>176</td>\n",
       "      <td>50</td>\n",
       "      <td>81</td>\n",
       "      <td>82</td>\n",
       "      <td>5.816667</td>\n",
       "      <td>294.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>227266</th>\n",
       "      <td>377885</td>\n",
       "      <td>1501</td>\n",
       "      <td>362</td>\n",
       "      <td>235</td>\n",
       "      <td>64</td>\n",
       "      <td>120</td>\n",
       "      <td>119</td>\n",
       "      <td>5.966667</td>\n",
       "      <td>169.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>163230</th>\n",
       "      <td>194401</td>\n",
       "      <td>877</td>\n",
       "      <td>13</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4.950000</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240192</th>\n",
       "      <td>365891</td>\n",
       "      <td>1013</td>\n",
       "      <td>24</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>4.733333</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>208691 rows × 47 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:05:36.384663Z",
     "start_time": "2024-11-28T12:05:33.016263Z"
    }
   },
   "cell_type": "code",
   "source": "model_lgb = lgb_train(X_train.values, y_train, X_valid.values, y_valid, verbose=True)",
   "id": "fcfec348c180ce69",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Number of positive: 12705, number of negative: 195986\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.009859 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6440\n",
      "[LightGBM] [Info] Number of data points in the train set: 208691, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.060879 -> initscore=-2.736048\n",
      "[LightGBM] [Info] Start training from score -2.736048\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[101]\ttraining's auc: 0.732105\ttraining's binary_logloss: 0.208892\tvalid_1's auc: 0.673069\tvalid_1's binary_logloss: 0.220438\n",
      "0.6730688147216816\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:06:18.405789Z",
     "start_time": "2024-11-28T12:06:17.221002Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "prob = model_lgb.predict_proba(test_data.values) # 预测\n",
    "submission = pd.read_csv('test_format1.csv')\n",
    " \n",
    "# 复购的概率\n",
    "submission['prob'] = pd.Series(prob[:,1]) # 预测数据赋值给提交数据\n",
    "display(submission.head())\n",
    "submission.to_csv('submission_lgb.csv', index=False)\n",
    " \n",
    "del submission\n",
    "gc.collect()"
   ],
   "id": "507d930494bded74",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id      prob\n",
       "0   163968         4605  0.056071\n",
       "1   360576         1581  0.095556\n",
       "2    98688         1964  0.041977\n",
       "3    98688         3645  0.034028\n",
       "4   295296         3361  0.067363"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>prob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>0.056071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>0.095556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98688</td>\n",
       "      <td>1964</td>\n",
       "      <td>0.041977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98688</td>\n",
       "      <td>3645</td>\n",
       "      <td>0.034028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>295296</td>\n",
       "      <td>3361</td>\n",
       "      <td>0.067363</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 2 s\n",
      "Wall time: 1.18 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1146"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:30:22.685397Z",
     "start_time": "2024-11-28T12:30:22.679781Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.metrics import roc_auc_score\n",
    "\n",
    "def xgb_train(X_train, y_train, X_valid, y_valid, verbose=True):\n",
    "    # 将目标变量中的浮点数字符串转换为整数\n",
    "    y_train = y_train.apply(lambda x: int(float(x)))\n",
    "    y_valid = y_valid.apply(lambda x: int(float(x)))\n",
    "\n",
    "    # 设置参数\n",
    "    params = {\n",
    "        'max_depth': 10,\n",
    "        'learning_rate': 0.1,\n",
    "        'objective': 'binary:logistic',\n",
    "        'min_child_weight': 300,\n",
    "        'colsample_bytree': 0.7,\n",
    "        'subsample': 0.9,\n",
    "        'silent': 1 if not verbose else 0\n",
    "    }\n",
    "    \n",
    "    # 创建 XGBClassifier 对象\n",
    "    model_xgb = xgb.XGBClassifier(**params)\n",
    "    \n",
    "    # 手动实现早停逻辑\n",
    "    best_auc = 0\n",
    "    best_model = None\n",
    "    no_improve_count = 0\n",
    "    patience = 10  # 早停的耐心轮数\n",
    "    num_boost_round = 5000  # 最大迭代次数\n",
    "\n",
    "    for i in range(num_boost_round):\n",
    "        # 训练模型\n",
    "        model_xgb.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_valid, y_valid)], verbose=False)\n",
    "        \n",
    "        # 计算验证集上的 AUC\n",
    "        y_pred_proba = model_xgb.predict_proba(X_valid)[:, 1]\n",
    "        auc = roc_auc_score(y_valid, y_pred_proba)\n",
    "        \n",
    "        if auc > best_auc:\n",
    "            best_auc = auc\n",
    "            best_model = model_xgb\n",
    "            no_improve_count = 0\n",
    "        else:\n",
    "            no_improve_count += 1\n",
    "        \n",
    "        if no_improve_count >= patience:\n",
    "            break\n",
    "    \n",
    "    if best_model:\n",
    "        print(f\"Best AUC: {best_auc}\")\n",
    "        return best_model\n",
    "    else:\n",
    "        return model_xgb"
   ],
   "id": "6fb313527dd6c0cd",
   "outputs": [],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:30:44.128969Z",
     "start_time": "2024-11-28T12:30:24.749847Z"
    }
   },
   "cell_type": "code",
   "source": "model_xgb = xgb_train(X_train, y_train, X_valid, y_valid, verbose=False)",
   "id": "a540558e4b41a7d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best AUC: 0.6706481744660022\n"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:30:48.956457Z",
     "start_time": "2024-11-28T12:30:48.374364Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "prob = model_xgb.predict_proba(test_data)\n",
    "submission = pd.read_csv('test_format1.csv')\n",
    "submission['prob'] = pd.Series(prob[:,1])\n",
    "submission.to_csv('submission_xgb.csv', index=False)\n",
    "display(submission.head())\n",
    "del submission\n",
    "gc.collect()"
   ],
   "id": "ed86657a49330651",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user_id  merchant_id      prob\n",
       "0   163968         4605  0.053662\n",
       "1   360576         1581  0.085872\n",
       "2    98688         1964  0.036131\n",
       "3    98688         3645  0.034655\n",
       "4   295296         3361  0.074432"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>prob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163968</td>\n",
       "      <td>4605</td>\n",
       "      <td>0.053662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>360576</td>\n",
       "      <td>1581</td>\n",
       "      <td>0.085872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98688</td>\n",
       "      <td>1964</td>\n",
       "      <td>0.036131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98688</td>\n",
       "      <td>3645</td>\n",
       "      <td>0.034655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>295296</td>\n",
       "      <td>3361</td>\n",
       "      <td>0.074432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 3.27 s\n",
      "Wall time: 578 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "631"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:31:34.050664Z",
     "start_time": "2024-11-28T12:31:34.046547Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "\n",
    "# 构造训练集和测试集\n",
    "def get_train_test_datas(train_df,label_df):\n",
    "    skv = StratifiedKFold(n_splits=10, shuffle=True)\n",
    "    trainX = []\n",
    "    trainY = []\n",
    "    testX = []\n",
    "    testY = []\n",
    "    # 索引：训练数据索引train_index,目标值的索引test_index\n",
    "    for train_index, test_index in skv.split(X=train_df, y=label_df):  # 10轮for循环\n",
    "        \n",
    "        train_x, train_y, test_x, test_y = train_df.iloc[train_index, :], label_df.iloc[train_index], \\\n",
    "                                            train_df.iloc[test_index, :], label_df.iloc[test_index]\n",
    " \n",
    "        trainX.append(train_x)\n",
    "        trainY.append(train_y)\n",
    "        testX.append(test_x)\n",
    "        testY.append(test_y)\n",
    "    return trainX, testX, trainY, testY"
   ],
   "id": "38815f710a3090d2",
   "outputs": [],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:39:24.636297Z",
     "start_time": "2024-11-28T12:38:11.328566Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']\n",
    "\n",
    "# 拆分为10份训练数据和验证数据\n",
    "X_train, X_valid, y_train, y_valid = get_train_test_datas(train_X, train_y)\n",
    "\n",
    "print('----训练数据，长度', len(X_train))\n",
    "print('----验证数据，长度', len(X_valid))\n",
    "\n",
    "pred_lgbms = []  # 列表，接受目标值，10轮，平均值\n",
    "\n",
    "from lightgbm import early_stopping\n",
    "\n",
    "for i in range(10):\n",
    "    print('\\n=========LGB training use Data {}/10===========\\n'.format(i+1))\n",
    "    model_lgb = lgb.LGBMClassifier(\n",
    "        max_depth=10,  # 8\n",
    "        n_estimators=1000,\n",
    "        min_child_weight=100,\n",
    "        colsample_bytree=0.7,\n",
    "        subsample=0.9,\n",
    "        learning_rate=0.05)\n",
    "    \n",
    "    callbacks = [early_stopping(stopping_rounds=10)]  # 使用 early_stopping 回调函数\n",
    "    \n",
    "    model_lgb.fit(\n",
    "        X_train[i], \n",
    "        y_train[i],\n",
    "        eval_metric='auc',\n",
    "        eval_set=[(X_train[i], y_train[i]), (X_valid[i], y_valid[i])],\n",
    "        callbacks=callbacks)\n",
    "    \n",
    "    print(model_lgb.best_score_['valid_1']['auc'])\n",
    "    pred = model_lgb.predict_proba(test_data)\n",
    "    pred = pd.DataFrame(pred[:, 1])  # 将预测概率（复购）去处理，转换成DataFrame\n",
    "    pred_lgbms.append(pred)\n",
    "\n",
    "# 求10轮平均值生成预测结果，保存\n",
    "# 每一轮的结果，作为一列，进行了添加\n",
    "pred_lgbms = pd.concat(pred_lgbms, axis=1)  # 级联，列进行级联\n",
    "\n",
    "# 加载提交数据\n",
    "submission = pd.read_csv('test_format1.csv')\n",
    "submission['prob'] = pred_lgbms.mean(axis=1)  # 10轮训练的平均值\n",
    "submission.to_csv('submission_KFold_lgb.csv', index=False)"
   ],
   "id": "61a64c717db7c7a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----训练数据，长度 10\n",
      "----验证数据，长度 10\n",
      "\n",
      "=========LGB training use Data 1/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220420\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.010771 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6436\n",
      "[LightGBM] [Info] Number of data points in the train set: 234777, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061152 -> initscore=-2.731297\n",
      "[LightGBM] [Info] Start training from score -2.731297\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[148]\ttraining's auc: 0.717309\ttraining's binary_logloss: 0.212109\tvalid_1's auc: 0.677691\tvalid_1's binary_logloss: 0.217377\n",
      "0.6776910840824744\n",
      "\n",
      "=========LGB training use Data 2/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220420\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.010913 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6443\n",
      "[LightGBM] [Info] Number of data points in the train set: 234777, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061152 -> initscore=-2.731297\n",
      "[LightGBM] [Info] Start training from score -2.731297\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[303]\ttraining's auc: 0.742258\ttraining's binary_logloss: 0.207709\tvalid_1's auc: 0.687183\tvalid_1's binary_logloss: 0.216192\n",
      "0.6871826102004007\n",
      "\n",
      "=========LGB training use Data 3/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14356, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.014928 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6444\n",
      "[LightGBM] [Info] Number of data points in the train set: 234777, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061147 -> initscore=-2.731371\n",
      "[LightGBM] [Info] Start training from score -2.731371\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[245]\ttraining's auc: 0.733325\ttraining's binary_logloss: 0.209322\tvalid_1's auc: 0.680535\tvalid_1's binary_logloss: 0.216924\n",
      "0.6805348371541323\n",
      "\n",
      "=========LGB training use Data 4/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14356, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.028939 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6437\n",
      "[LightGBM] [Info] Number of data points in the train set: 234777, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061147 -> initscore=-2.731371\n",
      "[LightGBM] [Info] Start training from score -2.731371\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[174]\ttraining's auc: 0.724147\ttraining's binary_logloss: 0.210891\tvalid_1's auc: 0.674935\tvalid_1's binary_logloss: 0.218359\n",
      "0.6749345496361049\n",
      "\n",
      "=========LGB training use Data 5/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.024975 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6436\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[177]\ttraining's auc: 0.723869\ttraining's binary_logloss: 0.210926\tvalid_1's auc: 0.675756\tvalid_1's binary_logloss: 0.217905\n",
      "0.6757560099167642\n",
      "\n",
      "=========LGB training use Data 6/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.025216 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6433\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[189]\ttraining's auc: 0.726244\ttraining's binary_logloss: 0.21061\tvalid_1's auc: 0.66988\tvalid_1's binary_logloss: 0.217948\n",
      "0.6698797037463318\n",
      "\n",
      "=========LGB training use Data 7/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.026604 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6439\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[199]\ttraining's auc: 0.725743\ttraining's binary_logloss: 0.210613\tvalid_1's auc: 0.685907\tvalid_1's binary_logloss: 0.216329\n",
      "0.685906600710209\n",
      "\n",
      "=========LGB training use Data 8/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.013726 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6430\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[210]\ttraining's auc: 0.727681\ttraining's binary_logloss: 0.210301\tvalid_1's auc: 0.690084\tvalid_1's binary_logloss: 0.215389\n",
      "0.6900841957297601\n",
      "\n",
      "=========LGB training use Data 9/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.014451 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6440\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[190]\ttraining's auc: 0.724915\ttraining's binary_logloss: 0.210676\tvalid_1's auc: 0.677576\tvalid_1's binary_logloss: 0.217609\n",
      "0.6775758582674283\n",
      "\n",
      "=========LGB training use Data 10/10===========\n",
      "\n",
      "[LightGBM] [Info] Number of positive: 14357, number of negative: 220421\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.015661 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 6442\n",
      "[LightGBM] [Info] Number of data points in the train set: 234778, number of used features: 46\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.061151 -> initscore=-2.731302\n",
      "[LightGBM] [Info] Start training from score -2.731302\n",
      "Training until validation scores don't improve for 10 rounds\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "Early stopping, best iteration is:\n",
      "[238]\ttraining's auc: 0.734328\ttraining's binary_logloss: 0.209225\tvalid_1's auc: 0.690475\tvalid_1's binary_logloss: 0.216066\n",
      "0.6904748964785093\n",
      "CPU times: total: 8min 14s\n",
      "Wall time: 1min 13s\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-28T12:40:13.545116Z",
     "start_time": "2024-11-28T12:40:13.541865Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构造训练集和测试集\n",
    "def get_train_test_datas(train_df,label_df):\n",
    "    skv = StratifiedKFold(n_splits=20, shuffle=True)\n",
    "    trainX = []\n",
    "    trainY = []\n",
    "    testX = []\n",
    "    testY = []\n",
    "    # 索引：训练数据索引train_index,目标值的索引test_index\n",
    "    for train_index, test_index in skv.split(X=train_df, y=label_df):# 10轮for循环\n",
    "        \n",
    "        train_x, train_y, test_x, test_y = train_df.iloc[train_index, :], label_df.iloc[train_index], \\\n",
    "                                           train_df.iloc[test_index, :], label_df.iloc[test_index]\n",
    " \n",
    "        trainX.append(train_x)\n",
    "        trainY.append(train_y)\n",
    "        testX.append(test_x)\n",
    "        testY.append(test_y)\n",
    "    return trainX, testX, trainY, testY"
   ],
   "id": "ec515c5f45a08d88",
   "outputs": [],
   "execution_count": 109
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2024-11-28T13:01:05.213703Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%%time\n",
    "train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']\n",
    "\n",
    "# 将目标变量中的浮点数字符串转换为整数\n",
    "train_y = train_y.apply(lambda x: int(float(x)))\n",
    "\n",
    "# 拆分为20份训练数据和验证数据\n",
    "X_train, X_valid, y_train, y_valid = get_train_test_datas(train_X, train_y)\n",
    "\n",
    "print('------数据长度', len(X_train), len(y_train))\n",
    "\n",
    "pred_xgbs = []\n",
    "for i in range(20):\n",
    "    print('\\n============XGB training use Data {}/20========\\n'.format(i+1))\n",
    "    model_xgb = xgb.XGBClassifier(\n",
    "        max_depth=10,  # raw8\n",
    "        n_estimators=5000,\n",
    "        min_child_weight=200, \n",
    "        colsample_bytree=0.7, \n",
    "        subsample=0.9,\n",
    "        learning_rate=0.1)\n",
    "\n",
    "    # 初始化早停参数\n",
    "    best_auc = 0\n",
    "    no_improvement_count = 0\n",
    "    early_stopping_rounds = 10\n",
    "\n",
    "    # 训练模型\n",
    "    for round in range(1, 5001):\n",
    "        model_xgb.fit(\n",
    "            X_train[i], \n",
    "            y_train[i],\n",
    "            eval_set=[(X_train[i], y_train[i]), (X_valid[i], y_valid[i])],\n",
    "            verbose=False\n",
    "        )\n",
    "        \n",
    "        # 计算验证集上的AUC\n",
    "        y_pred_proba = model_xgb.predict_proba(X_valid[i])[:, 1]\n",
    "        auc = roc_auc_score(y_valid[i], y_pred_proba)\n",
    "        \n",
    "        if auc > best_auc:\n",
    "            best_auc = auc\n",
    "            no_improvement_count = 0\n",
    "        else:\n",
    "            no_improvement_count += 1\n",
    "        \n",
    "        if no_improvement_count >= early_stopping_rounds:\n",
    "            print(f\"Early stopping triggered at round {round}.\")\n",
    "            break\n",
    "    \n",
    "    print(f\"Best AUC for fold {i+1}: {best_auc}\")\n",
    "    \n",
    "    # 使用最佳模型进行预测\n",
    "    pred = model_xgb.predict_proba(test_data)[:, 1]\n",
    "    pred_df = pd.DataFrame(pred)\n",
    "    pred_xgbs.append(pred_df)\n",
    "\n",
    "# 求20轮平均值生成预测结果，保存\n",
    "pred_xgbs = pd.concat(pred_xgbs, axis=1)\n",
    "submission = pd.read_csv('test_format1.csv')\n",
    "submission['prob'] = pred_xgbs.mean(axis=1)\n",
    "submission.to_csv('submission_KFold_xgb.csv', index=False)"
   ],
   "id": "2a22f498b404f14c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------数据长度 20 20\n",
      "\n",
      "============XGB training use Data 1/20========\n",
      "\n"
     ]
    }
   ],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
